package com.fly.glide;

import android.content.Context;
import android.os.Environment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.integration.okhttp.OkHttpGlideModule;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.engine.cache.ExternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;

import java.io.File;

/**
 * Created with Android Studio.
 * Package_Name：com.fly.glidedemo
 * Project_Name：GlideDemo
 * User：郭鹏飞
 * Date：2015/12/4
 * Email：love518420@foxmail.com
 * Description：
 */
public class BaseGlideModule extends OkHttpGlideModule {

    private static final String CACHE_DIR_NAME = "cache";

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        // 设置配置信息
        // 设置图片格式
        builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
        // 进行SDCard判断
        /*
        * isExternalStorageEmulated()设备的外存是否是用内存模拟的，是则返回true。(API Level 11)
        * isExternalStorageRemovable()设备的外存是否是可以拆卸的，比如SD卡，是则返回true。(API Level 9)
        * */
        DiskCache.Factory factory;
        String cachePath;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
                || !Environment.isExternalStorageRemovable()) {
            cachePath = AppUtils.getPackageName() + File.separator +
                    CACHE_DIR_NAME + File.separator + Constant.CACHE_DIR;
            switch (Constant.TYPE_CACHE_DIR) {
                case Constant.TYPE_CUSTOM_CACHE_DIR:
                    // 路径  sdcard/packagename/cache/image_manager_disk_cache
                    factory = new CustomExternalCacheDiskCacheFactory(
                            cachePath,
                            Constant.MAX_SIZE);
                    break;
                case Constant.TYPE_GLIDE_CACHE_DIR:
                default:
                    // 路径 sdcard/android/data/packagename/cache/image_manager_disk_cache
                    factory = new ExternalCacheDiskCacheFactory(
                            GlideApplication.getInstance(),
                            CACHE_DIR_NAME + File.separator + Constant.CACHE_DIR,
                            Constant.MAX_SIZE
                    );

            }
        } else {
            cachePath = AppUtils.getPackageName() +
                    File.separator + CACHE_DIR_NAME + File.separator + Constant.CACHE_DIR;
            // 路径 data/data/cache/packagename/cache/image_manager_disk_cache
            factory = new InternalCacheDiskCacheFactory(
                    GlideApplication.getInstance(),
                    cachePath,
                    Constant.MAX_SIZE);
        }
        // 设置缓存目录
        builder.setDiskCache(factory);
        // 设置内存
//        builder.setMemoryCache(new LruResourceCache());
    }

    @Override
    public void registerComponents(Context context, Glide glide) {

    }
}
